package cn.gzsxt.controller;

import static org.junit.Assert.*;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;

import com.alibaba.fastjson.JSON;

import cn.gzsxt.po.Constract;
import cn.gzsxt.po.SysUser;
import cn.gzsxt.service.ConstractService;
import cn.gzsxt.service.impl.ConstractServiceImpl;
import cn.gzsxt.vo.MsgObj;

//合同管理Controller : 添加 addConstract , listConstract, updateConstract,deleteConstract
@WebServlet("/constract.gzsxt")
public class ConstractController extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 引入service层
	private ConstractService service = new ConstractServiceImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 接受标识参数
		String cmd = req.getParameter("cmd");
		if ("addConstract".equals(cmd)) {
			addConstract(req, resp);
		}
	}

	private void addConstract(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 接受请求参数 : 房间id,租户id,物业类型id,用户id,起租日期,停组日期,签约日期,租金,押金,备注
		Constract constract = this.getBean(req, Constract.class);
		System.out.println(constract);
		//调用业务层保存和方法
		int  row =   service.addConstract(constract);
		//创建消息对象 
		MsgObj msgObj = new MsgObj(0,"添加合同失败");
		if(row == 1){
			msgObj = new MsgObj(1,"添加合同成功");
		}
		resp.setContentType("application/json;charset=utf-8");
		// 将list集合转换成json格式的字符串响应给前台
		resp.getWriter().print(JSON.toJSONString(msgObj));
	}
	/**
	 * 简单封装一个将请求对象中的所有参数封装成对象的方法
	 * @param request 请求对象.用户获取参数
	 * @param clz 封装成指定对象的Class
	 * @return clz 的实例对象
	 */
	public <T> T getBean(HttpServletRequest request, Class<T> clz) {
		T obj = null;
		try {
			obj = clz.newInstance();

			PropertyDescriptor[] pds = Introspector.getBeanInfo(clz,
					Object.class).getPropertyDescriptors();
			for (PropertyDescriptor pd : pds) {
				String name = pd.getName();
				// 通过属性名接受参数
				String parameter = request.getParameter(name);
				System.out.println("参数名: " + name + ", 值 :" + parameter);
				Class<?> type = pd.getPropertyType();

				System.out.println(type);
				if (parameter != null) {// 有此参数
					// 获取setter方法
					Method setter = pd.getWriteMethod();

					if (type.equals(Double.class) || type.equals(int.class)) {
						setter.invoke(obj, Double.valueOf((String) parameter));
					} else if (type.equals(Integer.class)
							|| type.equals(int.class)) {
						setter.invoke(obj, Integer.valueOf((String) parameter));
					} else if (type.equals(java.sql.Date.class)) {
						// 格式化日期
						DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
						java.util.Date date = format.parse(parameter);
						setter.invoke(obj, new Date(date.getTime()));
					} else if (type.equals(java.util.Date.class)) {
						// 格式化日期
						DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
						java.util.Date date = format.parse(parameter);
						setter.invoke(obj, date);
					} else {
						setter.invoke(obj, parameter);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Test
	public void testName() throws Exception {

	}
}
